home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3243 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: wv.mentorg.com!bloom
  2. From: bloom@wv.mentorg.com (Scott Aron Bloom)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloading conversion
  5. Date: 23 Jan 1996 00:27:11 GMT
  6. Organization: Mentor Graphics, Wilsonville, Oregon, USA
  7. Distribution: world
  8. Message-ID: <4e1a0v$dvh@hpbab.wv>
  9. References: <4d934v$10hs@ds2.acs.ucalgary.ca>
  10. NNTP-Posting-Host: glock.wv
  11.  
  12. In article <4d934v$10hs@ds2.acs.ucalgary.ca>, hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
  13. |> Is there a way to overload functions such that certain
  14. |> functions are called depending on the context the object are
  15. |> used. For example,
  16. |> 
  17. |> class T {
  18. |>   public:
  19. |>     int& int_value() { modified = TRUE; return value;}
  20. |>     int int_value() {return value;}
  21. |>  private:
  22. |>     int value;
  23. |>     int modified;
  24. |> }
  25. |> 
  26. |> 
  27. |> ...
  28. |> 
  29. |> T a;
  30. |> int test = a + 6 // int int_value should be called;
  31. |> a = 20 // int& int_value should be called.
  32. |> 
  33. |> However, this does not work because in both case int& int_value
  34. |> is called.  Is there another way to find out if value is changed
  35. |> or it's just inspected.
  36. |> 
  37.  
  38.  
  39. To tell you the truth, I am kinda shocked that class T compiled 
  40. at all.
  41.  
  42. My compiler gives the following error message:
  43.  
  44. CC: "t.C", line 4: error: two different return value types for T::int_value():  int & and  int  (1102)
  45. CC: "t.C", line 4: error: two definitions of T::int_value() (1034)
  46. CC: "t.C", line 4: error:  T::int_value() cannot be redeclared in class declaration (1168)
  47.  
  48.  
  49. And this is what i would expect.  Remember, return values from functions in 
  50. C/C++ are not required to be used.  Therefor you can not overload in C++
  51. based off of the return type.
  52.  
  53. What you really want is something like PASCAL has, the procedure/function
  54. difference.
  55.  
  56. While I have tried to figure out ways to do what you want, I usually 
  57. give up.  The method I sometimes use is having a member function T::value().
  58.  
  59. So for your code you would use
  60.  
  61. T a;
  62. int test = a.value + 6;
  63. a = 20;
  64.  
  65.  
  66. Sorry to be the bearer of bad news...
  67.  
  68. Scott
  69.  
  70. -- 
  71. ----------------------------------------------------------
  72. Scott Aron Bloom              email:  bloom@wv.mentorg.com
  73. Mentor Graphics Corporation   main:   (503) 685-7000
  74. 8005 S.W. Boeckman Road C/4   direct: (503) 685-7996
  75. Wilsonville, OR 97070-7777    fax:    (503) 685-1268
  76. ===============  Silicon Systems Division  ===============
  77. ----------------------------------------------------------
  78.